package com.tansun.easycare.rule.rulelog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tansun.easycare.rule.monitor.service.RuleMonitorInfoService;
import org.drools.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.core.config.Global;
import com.tansun.easycare.rule.rulelog.entity.RuleLogDetails;
import com.tansun.easycare.rule.rulelog.entity.RuleLogs;
import com.tansun.easycare.rule.rulelog.service.RuleLogDetailsService;
import com.tansun.easycare.rule.rulelog.service.RuleLogsService;
import com.tansun.rule.redis.RedisService;

@Component
public class LogTimeSchedule {
	
	public static final String LOG_CLASSNAME_1 ="acc";
	
	public static final String LOG_PARAMNAME_1_1 = "finalResult";
	
	public static final String LOG_PARAMNAME_1_2 = "errorCodes";
	
	@Autowired
	private RedisService redisService;
	
	@Autowired
	private RuleLogsService ruleLogsService;
	@Autowired
	RuleLogDetailsService RuleLogDetailsService;
	@Autowired
	RuleMonitorInfoService ruleMonitorInfoService;
	
    @Scheduled(cron="0 */1 * * * ?")
    public void updateLog() {
    	String isenable = Global.getConfig("redis.isenable");
    	if(!"1".equals(isenable))
    		return;
    	System.out.println("收集日志-start");
    	List<String> redisDataLogs = redisService.lpop("rule_logs",200);
    	if(redisDataLogs!=null && redisDataLogs.size()>0) {
        	List<RuleLogs> logs = new ArrayList<RuleLogs>();
        	for (String redisLogs : redisDataLogs) {
        		if(StringUtils.isEmpty(redisLogs))
        			continue;
        		RuleLogs ruleLog = JSON.parseObject(redisLogs, RuleLogs.class);
	    		if("1".equals(ruleLog.getIssuccess())) {
	    			parseRuleLog(ruleLog);
	    		}
        		logs.add(ruleLog);
    		}
        	ruleLogsService.insertBatch(logs, true);
    	}
    	List<List<String>> redisDataDetailLogs = redisService.lpopList("rule_log_details",200);
    	List<RuleLogDetails> details = new ArrayList<RuleLogDetails>();
    	for (List<String> list : redisDataDetailLogs) {
    		if(list==null || list.size()==0)
        		continue;
	    	for (String string : list) {
	    		RuleLogDetails detail = JSON.parseObject(string, RuleLogDetails.class);
	    		details.add(detail);    		
			}
		}
    	if(details.size()>0) {
    		RuleLogDetailsService.insertBatch(details);
    	}
    	//如果数据存储失败，重新塞回redis，等待下次被获取
//    	redisService.lPushAll("rule_logs", redisDataLogs);
//    	redisService.lPushAll("rule_log_details",redisDataDetailLogs);
    	System.out.println("收集日志-end");
    }
	//每天2点  0 0 2 * * ? 
//    每隔5秒执行一次：*/5 * * * * ?
//    每隔1分钟执行一次：0 */1 * * * ?
//    每天23点执行一次：0 0 23 * * ?
//    每天凌晨1点执行一次：0 0 1 * * ?
//    每月1号凌晨1点执行一次：0 0 1 1 * ?
//    每月最后一天23点执行一次：0 0 23 L * ?
//    每周星期天凌晨1点实行一次：0 0 1 ? * L
//    在26分、29分、33分执行一次：0 26,29,33 * * * ?
//    每天的0点、13点、18点、21点都执行一次：0 0 0,13,18,21 * * ?
    
    @SuppressWarnings("unchecked")
	public void parseRuleLog(RuleLogs ruleLogs) {
    	String ruleResult = ruleLogs.getRuleResult();
		Map<String,Object> resMap = JSON.parseObject(ruleResult,Map.class);
		if( resMap.containsKey(LOG_CLASSNAME_1) ) {
			Map<String,Object> childMap = (Map<String, Object>) resMap.get(LOG_CLASSNAME_1);
			//统计通过率
			if(childMap.containsKey(LOG_PARAMNAME_1_1)) {
				String finalResult = (String) childMap.get(LOG_PARAMNAME_1_1);
				ruleLogs.setFinalResult(finalResult);
			}
			//统计原因代码
			if(childMap.containsKey(LOG_PARAMNAME_1_2)) {
				List<String> reasonCodeList = (List<String>) childMap.get(LOG_PARAMNAME_1_2);
				if(reasonCodeList!=null&&reasonCodeList.size()>0) {
					ruleLogs.setReasonCodeList(JSON.toJSONString(reasonCodeList));
				}else {
					ruleLogs.setReasonCodeList("[]");
				}
			}
		}
    }

	//每隔10分钟执行一次：0 */10 * * * ?
	//@Scheduled(cron="0 */10 * * * ?")
	public void countNumberForMonitor(){
		String monitorDaysStr = Global.getConfig("monitor.days");
		Integer monitorDays = Integer.parseInt(monitorDaysStr);
    	ruleMonitorInfoService.countNumberForMonitor(monitorDays);
	}
}